package com.ruoyi.manager.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manager.domain.ArvinBookmark;
import com.ruoyi.manager.service.IArvinBookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 网站收藏Controller
 * 
 * @author ruoyi
 * @date 2023-07-11
 */
@RestController
@RequestMapping("/bookmark/bookmark")
public class ArvinBookmarkController extends BaseController
{
    @Autowired
    private IArvinBookmarkService arvinBookmarkService;

    /**
     * 查询网站收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArvinBookmark arvinBookmark)
    {
        // 设置当前登录用户ID，确保只能查询自己的数据
        arvinBookmark.setUserId(getUserId());
        
        startPage();
        List<ArvinBookmark> list = arvinBookmarkService.selectArvinBookmarkList(arvinBookmark);
        return getDataTable(list);
    }

    /**
     * 导出网站收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:export')")
    @Log(title = "网站收藏", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ArvinBookmark arvinBookmark)
    {
        // 设置当前登录用户ID，确保只能导出自己的数据
        arvinBookmark.setUserId(getUserId());
        
        List<ArvinBookmark> list = arvinBookmarkService.selectArvinBookmarkList(arvinBookmark);
        ExcelUtil<ArvinBookmark> util = new ExcelUtil<ArvinBookmark>(ArvinBookmark.class);
        return util.exportExcel(list, "bookmark");
    }

    /**
     * 获取网站收藏详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        // 通过ID获取收藏信息时需要验证是否属于当前用户
        ArvinBookmark bookmark = arvinBookmarkService.selectArvinBookmarkById(id);
        if (bookmark != null && !getUserId().equals(bookmark.getUserId())) {
            return AjaxResult.error("无权访问此收藏");
        }
        return AjaxResult.success(bookmark);
    }

    /**
     * 新增网站收藏
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:add')")
    @Log(title = "网站收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArvinBookmark arvinBookmark)
    {
        arvinBookmark.setUserId(getUserId());
        
        // 如果URL没有协议前缀，添加http://
        if (StringUtils.isNotEmpty(arvinBookmark.getUrl()) && 
            !arvinBookmark.getUrl().startsWith("http://") && 
            !arvinBookmark.getUrl().startsWith("https://")) {
            arvinBookmark.setUrl("http://" + arvinBookmark.getUrl());
        }
        
        // 如果没有排序号，设置默认值
        if (arvinBookmark.getSort() == null) {
            arvinBookmark.setSort(1);
        }
        
        return toAjax(arvinBookmarkService.insertArvinBookmark(arvinBookmark));
    }

    /**
     * 修改网站收藏
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:edit')")
    @Log(title = "网站收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArvinBookmark arvinBookmark)
    {
        // 修改前验证是否为当前用户的收藏
        ArvinBookmark original = arvinBookmarkService.selectArvinBookmarkById(arvinBookmark.getId());
        if (original != null && !getUserId().equals(original.getUserId())) {
            return AjaxResult.error("无权修改此收藏");
        }
        
        // 如果URL没有协议前缀，添加http://
        if (StringUtils.isNotEmpty(arvinBookmark.getUrl()) && 
            !arvinBookmark.getUrl().startsWith("http://") && 
            !arvinBookmark.getUrl().startsWith("https://")) {
            arvinBookmark.setUrl("http://" + arvinBookmark.getUrl());
        }
        
        return toAjax(arvinBookmarkService.updateArvinBookmark(arvinBookmark));
    }

    /**
     * 删除网站收藏
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:remove')")
    @Log(title = "网站收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 删除前验证是否为当前用户的收藏
        for (Long id : ids) {
            ArvinBookmark bookmark = arvinBookmarkService.selectArvinBookmarkById(id);
            if (bookmark != null && !getUserId().equals(bookmark.getUserId())) {
                return AjaxResult.error("无权删除他人的收藏");
            }
        }
        
        return toAjax(arvinBookmarkService.deleteArvinBookmarkByIds(ids));
    }
    
    /**
     * 搜索网站收藏
     */
//    @PreAuthorize("@ss.hasPermi('bookmark:bookmark:list')")
    @GetMapping("/search")
    public AjaxResult search(String keyword)
    {
        if (StringUtils.isEmpty(keyword)) {
            return AjaxResult.error("搜索关键词不能为空");
        }
        
        ArvinBookmark arvinBookmark = new ArvinBookmark();
        arvinBookmark.setTitle(keyword);
        // 设置当前登录用户ID，确保只能搜索自己的数据
        arvinBookmark.setUserId(getUserId());
        
        List<ArvinBookmark> list = arvinBookmarkService.selectArvinBookmarkList(arvinBookmark);
        
        return AjaxResult.success(list);
    }
} 